/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ControllerDentista;
import controller.ControllerEndereco;
import controller.ControllerPaciente;
import enun.EstadoCivil;
import enun.Perfil;
import enun.Sexo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.JOptionPane;
import model.Dentista;
import model.Endereco;
import model.Login;
import model.Paciente;
import util.MascaraUtil;
import util.MensagemUtil;

/**
 *
 * @author Celso Jr
 */
public class ViewDentista extends javax.swing.JFrame {

    private final ControllerDentista controllerDentista;
    private final ControllerEndereco controllerEndereco;
    private final Dentista dentista;
    private int num = 0;
    private boolean veriicaClickTabela = false;

    private Login login;

    public ViewDentista() {
        this(new Login());
    }

    public ViewDentista(Login login) {

        this.dentista = new Dentista();
        this.controllerDentista = new ControllerDentista();
        this.controllerEndereco = new ControllerEndereco();
        this.login = login;
        initComponents();
        lblLogado.setText(login.getPerfil().getDescricao());
        carregarComboEstadoCivil();
        carregarComboSexo();
        carregarEndereco();
        habilitaCampos();

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtCRO = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtNome = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDentista = new javax.swing.JTable();
        botaoCadastrar = new javax.swing.JToggleButton();
        botaoAlterar = new javax.swing.JToggleButton();
        botaoExcluir = new javax.swing.JToggleButton();
        comboEndereco = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        comboEstadoCivil = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        txtIdade = new javax.swing.JTextField();
        comboSexo = new javax.swing.JComboBox();
        btnSair = new javax.swing.JButton();
        lblLogado = new javax.swing.JLabel();
        lblusuarioLogado = new javax.swing.JLabel();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
        });

        jLabel1.setText("CRO:");

        txtCRO.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCROActionPerformed(evt);
            }
        });

        jLabel2.setText("Nome:");

        txtNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeActionPerformed(evt);
            }
        });

        jLabel3.setText("Endereço:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel4.setText("     Cadastro Dentista");

        tbDentista.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDentistaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbDentista);

        botaoCadastrar.setText("Cadastrar");
        botaoCadastrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoCadastrarActionPerformed(evt);
            }
        });

        botaoAlterar.setText("Alterar");
        botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAlterarActionPerformed(evt);
            }
        });

        botaoExcluir.setText("Excluir");
        botaoExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExcluirActionPerformed(evt);
            }
        });

        jLabel6.setText("Sexo:");

        jLabel7.setText("Estado Cívil:");

        jLabel8.setText("Idade:");

        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        lblLogado.setText("-");

        lblusuarioLogado.setText("Usuario logado:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(botaoCadastrar, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(35, 35, 35)
                        .addComponent(botaoAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(56, 56, 56)
                        .addComponent(botaoExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(comboEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, 519, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel2))
                                .addGap(0, 61, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel8))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(comboSexo, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(txtIdade, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel7)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(comboEstadoCivil, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel1)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtCRO))))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtNome)
                                        .addGap(20, 20, 20))))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSair))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(168, 168, 168)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(lblusuarioLogado)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(lblLogado, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(8, 8, 8)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblusuarioLogado)
                        .addComponent(lblLogado)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdade)
                    .addComponent(jLabel8)
                    .addComponent(jLabel1)
                    .addComponent(txtCRO, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(comboEstadoCivil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboSexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboEndereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botaoCadastrar)
                    .addComponent(botaoAlterar)
                    .addComponent(botaoExcluir))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair)
                .addGap(9, 9, 9))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNomeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNomeActionPerformed

    private void txtCROActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCROActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCROActionPerformed

    private void botaoCadastrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoCadastrarActionPerformed
        salvar();
    }//GEN-LAST:event_botaoCadastrarActionPerformed

    private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
        alterar();
    }//GEN-LAST:event_botaoAlterarActionPerformed

    private void botaoExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExcluirActionPerformed
        deletarDentista();

    }//GEN-LAST:event_botaoExcluirActionPerformed

    private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated
        carregarTable();
    }//GEN-LAST:event_formWindowActivated

    private void tbDentistaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDentistaMouseClicked
        carregarInformacaoTable(evt);
    }//GEN-LAST:event_tbDentistaMouseClicked

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void carregarComboSexo() {
        List<Sexo> listaSexo = new ArrayList<>();
        listaSexo = Arrays.asList(Sexo.values());
        comboSexo.removeAllItems();
        comboSexo.addItem("Selecione");
        for (Sexo sexo : listaSexo) {
            comboSexo.addItem(sexo);
        }
        comboSexo.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
    }

    private void carregarComboEstadoCivil() {
        List<EstadoCivil> listaEstadoCivil = new ArrayList<>();
        listaEstadoCivil = Arrays.asList(EstadoCivil.values());
        comboEstadoCivil.removeAllItems();
        comboEstadoCivil.addItem("Selecione");
        for (EstadoCivil estadoCivil : listaEstadoCivil) {
            comboEstadoCivil.addItem(estadoCivil);
        }
        comboEstadoCivil.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
    }

    private void carregarInformacoesTela() {

        if (veriicaClickTabela) {
            dentista.setId((int) tbDentista.getValueAt(num, 0));
        }
        MascaraUtil.limitarCampoTexto(txtNome, "Nome", 25);
        dentista.setNome(txtNome.getText());
        MascaraUtil.validaNumero(txtIdade, "Idade");
        MascaraUtil.limitarCampoTexto(txtIdade, "Idade", 3);
        dentista.setIdade(Integer.parseInt(txtIdade.getText()));
        MascaraUtil.validaNumero(txtCRO, "Nº CRO");
        MascaraUtil.limitarCampoTexto(txtCRO, "CRO", 8);
        dentista.setNumeroCRO(Integer.parseInt(txtCRO.getText()));
        dentista.setEstadoCivil((EstadoCivil) comboEstadoCivil.getSelectedItem());
        dentista.setSexo((Sexo) comboSexo.getSelectedItem());
        dentista.setEndereco((Endereco) comboEndereco.getSelectedItem());
    }

    private boolean validarCampos() {
        boolean isValido = true;

        if (txtNome.getText().equals("")) {
            isValido = false;
            MensagemUtil.msgPadrao("Nome");
        }

        if (txtCRO.getText().equals("")) {
            isValido = false;
            MensagemUtil.msgPadrao("CRO");
        }

        if (txtIdade.getText().equals("")) {
            isValido = false;
            MensagemUtil.msgPadrao("Idade");
        }

        if (comboEndereco.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Endereco");
        }

        if (comboEstadoCivil.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Estado Civil");
        }

        if (comboSexo.getSelectedItem().equals("Selecione")) {
            isValido = false;
            MensagemUtil.msgPadrao("Sexo");
        }
        return isValido;
    }

    private void salvar() {
        if (validarCampos()) {
            carregarInformacoesTela();
            controllerDentista.inserir(dentista);
            JOptionPane.showMessageDialog(null, " Registro Cadastrado com sucesso !!");
            limparTela();
            carregarTable();
        }

    }

    private void alterar() {
        if (veriicaClickTabela) {
            if (validarCampos()) {
                carregarInformacoesTela();
                controllerDentista.alterar(dentista);
                JOptionPane.showMessageDialog(null, "Registro alterado com sucesso!!!");
                veriicaClickTabela = false;
                limparTela();
                carregarTable();
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecione o Dentista na tabela para realizar a alteração !!!");
        }

    }

    private void carregarInformacaoTable(java.awt.event.MouseEvent evt) {

        num = tbDentista.getSelectedRow();
        if (evt.getClickCount() == 2) {
            veriicaClickTabela = true;
            txtNome.setText((String) tbDentista.getValueAt(num, 1));
            txtIdade.setText(String.valueOf(tbDentista.getValueAt(num, 2)));
            txtCRO.setText(String.valueOf((tbDentista.getValueAt(num, 3))));
            comboSexo.setSelectedItem(tbDentista.getValueAt(num, 4));
            comboEstadoCivil.setSelectedItem(tbDentista.getValueAt(num, 5));
            comboEndereco.setSelectedItem(tbDentista.getValueAt(num, 6));
        }
    }

    private void carregarTable() {
        TableDentista tb = new TableDentista();
        tb.fillingRows();
        tbDentista.setModel(tb);
    }

    private void limparTela() {
        txtNome.setText("");
        txtIdade.setText("");
        txtCRO.setText("");
        comboEndereco.setSelectedItem(null);
        comboEstadoCivil.setSelectedItem(null);
        comboSexo.setSelectedItem(null);
        carregarEndereco();
        carregarComboSexo();
        carregarComboEstadoCivil();

    }

    private void deletarDentista() {
        if (veriicaClickTabela) {
            String name = (String) tbDentista.getValueAt(num, 1);
            dentista.setId(retornaID());
            int status = JOptionPane.showConfirmDialog(null, "Tem certeza que deseja deletar o Dentista: " + name + " ?", "Confirmação", JOptionPane.YES_NO_OPTION);
            if (status == 0) {
                controllerDentista.deletar(dentista);
                JOptionPane.showMessageDialog(null, "Dentista excluido com sucesso.");
                veriicaClickTabela = false;
                limparTela();
            }
        } else {
            JOptionPane.showMessageDialog(null, "Selecione o Dentista na tabela para realizar a exclusão !!!");
        }

    }

    private void carregarEndereco() {
        List<Endereco> end = controllerEndereco.consultar();
        comboEndereco.removeAllItems();
        comboEndereco.addItem("Selecione");
        for (Endereco e : end) {
            comboEndereco.addItem(e);
        }
        comboEndereco.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
    }

    private int retornaID() {
        return (int) tbDentista.getValueAt(tbDentista.getSelectedRow(), 0);
    }

    private void habilitaCampos() {
        txtIdade.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
        txtNome.setEditable(Perfil.SECRETARIA_ADM == login.getPerfil());
        txtCRO.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
        botaoAlterar.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
        botaoCadastrar.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
        botaoExcluir.setEnabled(Perfil.SECRETARIA_ADM == login.getPerfil());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton botaoAlterar;
    private javax.swing.JToggleButton botaoCadastrar;
    private javax.swing.JToggleButton botaoExcluir;
    private javax.swing.JButton btnSair;
    private javax.swing.JComboBox comboEndereco;
    private javax.swing.JComboBox comboEstadoCivil;
    private javax.swing.JComboBox comboSexo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblLogado;
    private javax.swing.JLabel lblusuarioLogado;
    private javax.swing.JTable tbDentista;
    private javax.swing.JTextField txtCRO;
    private javax.swing.JTextField txtIdade;
    private javax.swing.JTextField txtNome;
    // End of variables declaration//GEN-END:variables
}
